﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	ViewQ1
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Data.SQLite;

namespace Map_Fvg
{

    partial class ViewQ
    {

        static public int isel_selected;
        static public int selected_linea;
        static public int selected_point;
        static public int selected_area;
        static int[] ipt;
        static public string[] lpt;
        static private int idiff;

        private void selezioneLinea(PointF Hit)
        {

            int isel = 0;

            int isel_max = 200;
            ipt = new int[isel_max];
            int i = 0;
            for (i = 0; i < nShapes; ++i)
            {
                int pntSimb = vectShapes[i].pntSimb;
                if (vectS[pntSimb].visib == 0) continue;
                if (Hit.X < vectShapes[i].PointA.X) continue;
                if (Hit.X > vectShapes[i].PointB.X) continue;
                if (Hit.Y < vectShapes[i].PointA.Y) continue;
                if (Hit.Y > vectShapes[i].PointB.Y) continue;
                vectShapes[i].selected = 1;
                ipt[isel] = i;
                ++isel;
                if (isel == isel_max)
                {
                    MessageBox.Show("superato limite vettore ipt :" + isel_max.ToString());
                    break;
                }
            }
            idiff = (int)(18.0 / scalaX);

            int X1 = (int)Hit.X - idiff;
            int Y1 = (int)Hit.Y - idiff;
            int X2 = (int)Hit.X + idiff;
            int Y2 = (int)Hit.Y + idiff;

            int nsel = 0;

            for (i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                int nVertices = vectShapes[j].nVertices;
                int ipnt = vectShapes[j].pntpL;
                int pntSimb = vectShapes[j].pntSimb;

                if (vectS[pntSimb].selected == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int visib = vectS[pntSimb].visib;
                if (visib == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int k = 0;
                for (k = 0; k < nVertices; ++k)
                {
                    int i1 = 0;
                    if (PL[ipnt].X > X1 && (PL[ipnt].X < X2)) ++i1;
                    if (PL[ipnt].Y > Y1 && (PL[ipnt].Y < Y2)) ++i1;
                    if (i1 == 2) break;
                    ++ipnt;

                }
                if (k < nVertices) ++nsel;
                else ipt[i] = -j;


            }

            if (nsel == 0)
            {
                MessageBox.Show("non trovata nessuna linea che comprenda il punto selezionato - 0");
                return;
            }

            dc0.RotateTransform(alpha);

            int iX1 = (int)((X1 - Xmin) * scalaX) + Xoff;
            int iY1 = (int)((Y2 - Ymin) * scalaY) + Yoff;
            int iXY = (int)(2 * idiff * scalaX);

            Rectangle pntR = new Rectangle(iX1, iY1, iXY, iXY);
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawRectangle(iPen, pntR);

            for (i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawLinea(j, 0);

            }
            dc0.ResetTransform();

            if (nsel > 1)
            {
                isel_selected = isel;
                idiff = (int)(10.0F / scalaX);
                return;
            }
            if (nsel == 1)
            {

                string caption = "selezione linea";
                var result = MessageBox.Show("è la linea cercata?", caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    int jj = 0;
                    for (i = 0; i < isel; ++i)
                    {
                        jj = ipt[i];
                        if (jj < 0) continue;
                        break;
                    }
                    selected_linea = jj;
                    //                  LineaSelect_flag = 1;
                    Select_flag = (int)flagSelez.linea;

                    Linea_dlg dlg = new Linea_dlg();
                    DialogResult dr = dlg.ShowDialog();
                    isel_selected = 0;
                    /*
                    if (dr == DialogResult.Yes)
                    {
                       panel1.Invalidate();
                        isel_selected = 0;
                    }
                  */
                }
            }
        }

        private void drawLinea(int ipt, int opt)
        {
            char[] delimiterChars0 = { ',', '/' };
            string[] words;


            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int nVertices = vectShapes[ipt].nVertices;
            int ipnt = vectShapes[ipt].pntpL;
            int pntSimb = vectShapes[ipt].pntSimb;

            if (vectS[pntSimb].selected == 0) goto L40;
            int visib = vectS[pntSimb].visib;
            if (visib == 0) goto L40;

            Color icolor = new Color();
            if (opt == 1) icolor = vectS[pntSimb].icolor;
            else icolor = Color.Red;
            string ss = vectS[pntSimb].tratto;
            int type = vectS[pntSimb].penType;
            float width = 0F;
            float interpasso = 0F;
            float diam = 0F;
            string s1;
            float[] dashValues = { 0, 0 };

            Scarpata scr = new Scarpata();

            switch (type)
            {
                case 1:
                    int ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    width = float.Parse(s1, nfi);
                    break;
                case 11:            // scarpata (ciglio)
                case 12:            // scarpata (piede)
                case 13:            // scarpata rivestita (ciglio)
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    width = float.Parse(words[3], nfi);
                    dashValues[0] = float.Parse(words[0], nfi);
                    dashValues[1] = float.Parse(words[1], nfi);
                    interpasso = float.Parse(words[2], nfi);

                    break;
                case 26:         //  limite di bosco
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    width = float.Parse(words[4], nfi);
                    dashValues[0] = float.Parse(words[0], nfi);
                    dashValues[1] = float.Parse(words[1], nfi);
                    diam = float.Parse(words[2], nfi);
                    interpasso = float.Parse(words[3], nfi);
                    break;
                case 33:
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    dashValues[0] = float.Parse(words[0], nfi);
                    dashValues[1] = float.Parse(words[1], nfi);
                    width = float.Parse(words[2], nfi);
                    break;
                case 40:         //  curva di livello intermedia
                case 41:         //  curva di livello direttrice
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    width = float.Parse(s1, nfi);
                    break;

                case 42:     //  sentieri CAI
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    width = float.Parse(words[0], nfi);
                    dashValues[0] = 0F;
                    break;
                case 43:        //  sentieri
                case 44:        //  bordo di rotabile
                case 60:        //  tracce sentieri                    
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    width = float.Parse(words[2], nfi);
                    dashValues[0] = float.Parse(words[0], nfi);
                    dashValues[1] = float.Parse(words[1], nfi);
                    break;
                default:
                    width = 0F;
                    break;
            }
            if (width == 0F) goto L40;
            /*        
                    if (vectS[pntSimb].penType == 1)
                    {
                        width = float.Parse(ss);
                    }
                    //            float width = vectS[pntSimb].penWidth;
                    */

            Pen iPen = new Pen(icolor, width);
            if (type != 12 && dashValues[0] > 0F) iPen.DashPattern = dashValues;

            Point[] polygon = new Point[nVertices];
            for (int j = 0; j < nVertices; ++j)
            {
                polygon[j].X = (int)((PL[ipnt].X - Xmin) * scalaX) + Xoff;
                polygon[j].Y = (int)((PL[ipnt].Y - Ymin) * scalaY) + Yoff;
                ++ipnt;
                Rectangle pntR = new Rectangle(polygon[j].X, polygon[j].Y, 4, 4);
                if (type != 12) dc0.DrawEllipse(iPen, pntR);
            }
            if (type == 11 || type == 12 || type == 13)            // scarpata (piede)
            {
                scr.scarpata(iPen, polygon, width, dashValues[0], dashValues[1], interpasso, dc0, ipt, type);
            }

            else if (type == 38)    //  limite di comune
            {
                dashValues[0] = diam;
                dashValues[1] = interpasso - diam;
                iPen.DashPattern = dashValues;
                dc0.DrawCurve(iPen, polygon);
            }

            else dc0.DrawCurve(iPen, polygon);

            if (type == 26 && diam > 0)        //  limite di bosco
            {
                scr.limiteDiBosco(iPen, polygon, width, dashValues[0], dashValues[1], interpasso, diam, dc0, ipt);
            }

        L40: ;

        }

        private void selezioneLinea1(Point Hit)
        {

            int isel = isel_selected;

            dc0.RotateTransform(alpha);

            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawLinea(j, 1);
            }
            dc0.ResetTransform();

            idiff -= (int)(3.0F / scalaX);

            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            int nsel = 0;
            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                int nVertices = vectShapes[j].nVertices;
                int ipnt = vectShapes[j].pntpL;
                int pntSimb = vectShapes[j].pntSimb;

                if (vectS[pntSimb].selected == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int visib = vectS[pntSimb].visib;
                if (visib == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int k = 0;
                for (k = 0; k < nVertices; ++k)
                {
                    int i1 = 0;
                    if (PL[ipnt].X > X1 && (PL[ipnt].X < X2)) ++i1;
                    if (PL[ipnt].Y > Y1 && (PL[ipnt].Y < Y2)) ++i1;
                    if (i1 == 2) break;
                    ++ipnt;

                }
                if (k < nVertices) ++nsel;
                else ipt[i] = -j;


            }

            //        MessageBox.Show(isel.ToString() + "  " + nsel.ToString());
            if (nsel == 0)
            {
                MessageBox.Show("non trovata nessuna linea che comprenda il punto selezionato - 1");
                isel_selected = 0;
                tipo_selezione = 0;

                return;
            }

            dc0.RotateTransform(alpha);

            Point Pt = new Point();
            Pt.X = (int)((X1 - Xmin) * scalaX) + Xoff;
            Pt.Y = (int)((Y2 - Ymin) * scalaY) + Yoff;
            Size sz = new Size(2 * idiff, 2 * idiff);
            Rectangle pntR = new Rectangle(Pt, sz);
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawRectangle(iPen, pntR);

            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawLinea(j, 0);

            }
            dc0.ResetTransform();


            if (nsel == 1)
            {

                string caption = "selezione linea";
                var result = MessageBox.Show("è la linea cercata?", caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    int jj = 0;
                    for (int i = 0; i < isel; ++i)
                    {
                        jj = ipt[i];
                        if (jj > 0) break;
                    }
                    selected_linea = jj;
                    Select_flag = (int)flagSelez.linea;

                    Linea_dlg dlg = new Linea_dlg();
                    DialogResult dr = dlg.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        panel1.Invalidate();
                        isel_selected = 0;
                    }
                }

            }

            if (nsel > 1)
            {
                isel_selected = nsel;

                lpt = new string[nsel];
                int j = 0;
                for (int i = 0; i < isel; ++i)
                {
                    int jj = ipt[i];
                    if (jj < 0) continue;
                    lpt[j] = jj.ToString();
                    ++j;
                }


                InfoDialog10 dlg = new InfoDialog10();
                DialogResult iret = dlg.ShowDialog();
                if (iret == DialogResult.OK)
                {

                    selected_linea = int.Parse(lpt[selElem]);
                    Select_flag = (int)flagSelez.linea;
                    Linea_dlg dlg1 = new Linea_dlg();
                    DialogResult dr = dlg1.ShowDialog();
                    if (dr == DialogResult.OK)
                    {
                        panel1.Invalidate();
                        isel_selected = 0;
                        tipo_selezione = 0;
                    }
                }
                else
                {
                    isel_selected = 0;
                    tipo_selezione = 0;
                    Select_flag = 0;
                }
            }
        }

        private void prova(Point Hit)
        {

            int idiff = (int)(15.0F / scalaX);
            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            dc0.RotateTransform(alpha);
            Point P1 = new Point();
            Point P2 = new Point();

            P1.X = (int)((X1 - Xmin) * scalaX) + Xoff;//
            P1.Y = (int)((Hit.Y - Ymin) * scalaY) + Yoff;//Y1

            P2.X = (int)((X2 - Xmin) * scalaX) + Xoff;//
            P2.Y = (int)((Hit.Y - Ymin) * scalaY) + Yoff;//Y1
            // MessageBox.Show(P1.ToString() + "  " + P2.ToString()); 
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawLine(iPen, P1, P2);

            P1.X = (int)((Hit.X - Xmin) * scalaX) + Xoff;//
            P1.Y = (int)((Y1 - Ymin) * scalaY) + Yoff;//Y1

            P2.X = (int)((Hit.X - Xmin) * scalaX) + Xoff;//
            P2.Y = (int)((Y2 - Ymin) * scalaY) + Yoff;//Y1

            dc0.DrawLine(iPen, P1, P2);


            dc0.ResetTransform();

        }

        private int selezioneNodo(Point Hit)
        {
            int isel = ViewQ.selected_linea;

            int nVertices = vectShapes[isel].nVertices;
            int ipnt = vectShapes[isel].pntpL;
            int pntSimb = vectShapes[isel].pntSimb;

            idiff = (int)(10.0 / scalaX);

            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            int i = 0;
            for (i = 0; i < nVertices; ++i)
            {
                int i1 = 0;
                if (PL[ipnt].X > X1 && (PL[ipnt].X < X2)) ++i1;
                if (PL[ipnt].Y > Y1 && (PL[ipnt].Y < Y2)) ++i1;
                if (i1 == 2) break;
                ++ipnt;

            }
            if (i >= nVertices - 1) return (0);
            if (i == 0) return (0);

            prova(Hit);
            return (i);
        }

        private int selectCircle(Point Hit)
        {
            int isel = selected_linea;
            int nVertices = ViewQ.vectShapes[isel].nVertices;
            int ipnt = vectShapes[isel].pntpL;

            idiff = 6; //(int)(3.0F / scalaX);

            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            int k = 0;
            for (k = 0; k < nVertices; ++k)
            {
                int i1 = 0;
                if (PL[ipnt].X > X1 && (PL[ipnt].X < X2)) ++i1;
                if (PL[ipnt].Y > Y1 && (PL[ipnt].Y < Y2)) ++i1;
                if (i1 == 2) break;
                ++ipnt;
            }
            if (k >= nVertices) return 0;
            selected_point = k + 1;

            dc0.RotateTransform(alpha);

            Point Pt = new Point();
            Pt.X = (int)((X1 - Xmin) * scalaX) + Xoff + idiff;
            Pt.Y = (int)((Y2 - Ymin) * scalaY) + Yoff + 2*idiff/3;          //  da verificare
            Size sz = new Size(2 * idiff, 2 * idiff);
            Rectangle pntR = new Rectangle(Pt, sz);
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawRectangle(iPen, pntR);

            dc0.ResetTransform();

            return 1;
        }

        private int selectCircle1(Point Hit)
        {
            int isel = selected_linea;
            int nVertices = ViewQ.vectShapes[isel].nVertices;
            int ipnt = vectShapes[isel].pntpL;

            idiff = 6; //(int)(3.0F / scalaX);

            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            int k = 0;
            for (k = 0; k < nVertices; ++k)
            {
                int i1 = 0;
                if (PL[ipnt].X > X1 && (PL[ipnt].X < X2)) ++i1;
                if (PL[ipnt].Y > Y1 && (PL[ipnt].Y < Y2)) ++i1;
                if (i1 == 2) break;
                ++ipnt;
            }
            if (k >= nVertices) return -1;
            selected_point = k;

            dc0.RotateTransform(alpha);

            Point Pt = new Point();
            Pt.X = (int)((X1 - Xmin) * scalaX) + Xoff;
            Pt.Y = (int)((Y2 - Ymin) * scalaY) + Yoff;
            Size sz = new Size(2 * idiff, 2 * idiff);
            Rectangle pntR = new Rectangle(Pt, sz);
            Pen iPen = new Pen(Color.Blue, 1);
            dc0.DrawRectangle(iPen, pntR);

            dc0.ResetTransform();

            return 1;
        }

        public static int newPnt;
        public static int newVertices;
        public static int error_flag = 0;

        private void inserimentoLinea(Point Hit)
        {

            prova(Hit);

            if (newPnt == 0)
            {
                if (inserimento_modo == 1)
                {
                    if (nShapes == max_nShapes)
                    {
                        MessageBox.Show("numero eccessivo di linee inserite; procedere al salvataggio");
                        error_flag = 1;
                        return;
                    }

                    int isel = nShapes - 1;
                    int nVertices = vectShapes[isel].nVertices;
                    newPnt = vectShapes[isel].pntpL + nVertices;
                    newVertices = 0;
                    vectShapes[nShapes].pntpL = newPnt;

                }
                if (inserimento_modo == 2)
                {
                    int isel = nShapes - 1;
                    int nVertices = vectShapes[isel].nVertices;
                    newPnt = vectShapes[isel].pntpL + nVertices;
                    newVertices = 0;
                }
                if (inserimento_modo == 3)
                {
                    int isel = nShapes - 1;
                    int nVertices = vectShapes[isel].nVertices;
                    newPnt = vectShapes[isel].pntpL + nVertices;
                    newVertices = 0;
                }
            }
            PL[newPnt].X = Hit.X;
            PL[newPnt].Y = Hit.Y;
            ++newPnt;
            ++newVertices;
            if (newPnt == max_nPoints)
            {
                MessageBox.Show("numero eccessivo di punti inseriti; procedere al salvataggio");
                error_flag = 1;
                return;
            }
        }

        private void inserimentoLinea1()
        {

            Select_flag = (int)flagSelez.linea;
            int last_linea = nShapes - 1;

            int nVertices = vectShapes[last_linea].nVertices;
            int lastPnt = vectShapes[last_linea].pntpL + nVertices - 1;
            Point Hit = new Point();

            Hit.X = PL[lastPnt].X;
            Hit.Y = PL[lastPnt].Y;

            prova(Hit);
            selected_linea = last_linea;
            insNodiDlg.insert_point = nVertices;
        }

        private void inserimentoLinea2()
        {
            if (newVertices == 0) return;
            Point[] PLL = new Point[newVertices];

            int last_linea = nShapes - 1;
            int nVertices = vectShapes[last_linea].nVertices;
            int j = vectShapes[last_linea].pntpL + nVertices;

            for (int i = 0; i < newVertices; ++i)
            {
                PLL[i] = PL[j];
                ++j;
            }

            int last_pnt = vectShapes[last_linea].pntpL + nVertices + newVertices - 1;

            int init = insNodiDlg.insert_point;
            int ipnt = vectShapes[selected_linea].pntpL + init;
            for (int i = last_pnt; i >= ipnt; --i) PL[i + newVertices] = PL[i];

            for (int i = 0; i < newVertices; ++i) PL[i + ipnt] = PLL[i];

            vectShapes[selected_linea].nVertices += newVertices;

            for (int i = selected_linea + 1; i <= last_linea; ++i)
            {
                vectShapes[i].pntpL += newVertices;
            }
        }


        public static int i_selected;
        static Control ctx = null;

        private void selezioneTesto(PointF Hit)
        {

            //         Point[] vert = new Point[4];
            int idiff = (int)(20.0F / scalaX);
            int idiff2 = idiff * idiff;

            int xdiff = 0, ydiff = 0;


            int i = 0;
            for (i = 0; i < nShapesT; ++i)
            {
                xdiff = vectShapesT[i].XX.X - (int)Hit.X;
                ydiff = vectShapesT[i].XX.Y - (int)Hit.Y;

                int isum2 = xdiff * xdiff + ydiff * ydiff;
                if (isum2 < idiff2) break;
            }

            if (i < nShapesT)
            {
                i_selected = i;
                //           TextSelect_flag = 1;
                Select_flag = (int)flagSelez.testo;

                DragDropHandler.RegisterControl(panel1, true, true);

                int pntSimb = vectShapesT[i].pntSimb;

                Color icolor = vectT[pntSimb].icolor;
                SolidBrush iBrush = new SolidBrush(icolor);
                Font iFont = vectT[pntSimb].iFont;

                float angle = -vectShapesT[i].GMrotation;
                string stx = vectShapesT[i].testo;
                float width1 = dc0.MeasureString(stx, iFont).Width + 16;
                float height1 = dc0.MeasureString(stx, iFont).Height + 6;

                MyControlTesto ctrl = new MyControlTesto();
                ctrl.Parent = panel1;
                ctrl.Visible = true;
                ctrl.colore = Color.Red;
                ctrl.myFont = iFont;
                ctrl.angolo = angle;
                width1 = height1;
                Size sz = new Size((int)width1, (int)height1);
                Point Pnt = new Point(0, 0);
                Pnt.X = (int)((vectShapesT[i].XX.X - Xmin) * scalaX) + Xoff;
                Pnt.Y = (int)((vectShapesT[i].XX.Y - Ymin) * scalaY) + Yoff;
                Pnt.Y -= (int)width1;
                ctrl.Location = Pnt;
                ctrl.Size = sz;
                ctrl.testo = stx;
                DragDropHandler.RegisterControl(ctrl, false, false);
                //         DragDrop1.Form1.m_IsDragging = true;
                ctx = ctrl;
                //             MessageBox.Show(stx + "   " + pt.X.ToString() + "  " + pt.Y.ToString());

            }
            else MessageBox.Show("I'm sorry");
        }

        public static Point HitM;

        private void selezioneTesto1(Point Hit)
        {

            i_selected = -1;
            HitM = Hit;
            SelezTesto dialog = new SelezTesto();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                panel1.Invalidate();
            }
        }


        private void selezionePoint(PointF Hit)
        {

            //         Point[] vert = new Point[4];
            int idiff = (int)(20.0F / scalaX);
            int idiff2 = idiff * idiff;

            int xdiff = 0, ydiff = 0;


            int i = 0;
            for (i = 0; i < nShapesP; ++i)
            {
                int pntSimb = vectShapesP[i].pntSimb;
                if (vectP[pntSimb].visib == 0) continue;
                xdiff = vectShapesP[i].XX.X - (int)Hit.X;
                ydiff = vectShapesP[i].XX.Y - (int)Hit.Y;

                int isum2 = xdiff * xdiff + ydiff * ydiff;
                if (isum2 < idiff2) break;
            }

            if (i < nShapesP)
            {
                i_selected = i;

                //              PointSelect_flag = 1;
                Select_flag = (int)flagSelez.punto;

                int pntSimb = vectShapesP[i].pntSimb;
                int codice = vectP[pntSimb].simbolo;
                if (codice == 0)
                {
                    MessageBox.Show("I'm sorry   " + vectP[pntSimb].denom.ToString());
                    return;
                }

                DragDropHandler.RegisterControl(panel1, true, true);

                Color icolor = vectP[pntSimb].icolor;
                SolidBrush iBrush = new SolidBrush(icolor);
                Font iFont = vectP[pntSimb].iFont;
                float angle = -vectShapesP[i].GMrotation;
                string fileName = vectP[pntSimb].fileName;

                string fileX = string.Empty;
                if (!string.IsNullOrEmpty(fileName))
                {
                    fileName = fileName.Trim();
                    fileX = FormIniz.bitmapFolder;
                    fileX = string.Concat(fileX, fileName);
                    string ss = vectP[pntSimb].paramSimb;

                }
                //         string stx = vectShapesP[i].testo;
                //          float width1 = dc0.MeasureString(stx, iFont).Width + 16;
                //          float height1 = dc0.MeasureString(stx, iFont).Height + 6;
                float width1 = 16;
                float height1 = 16;
                MyControlPoint ctrl = new MyControlPoint();
                ctrl.Parent = panel1;
                ctrl.Visible = true;
                ctrl.colore = Color.Red;
                ctrl.myFont = iFont;
                ctrl.angolo = angle;
                ctrl.fileImag = fileX;
                ctrl.codice = codice;
                width1 = height1;
                Size sz = new Size((int)width1, (int)height1);
                Point Pnt = new Point(0, 0);

                double alphaR = ViewQ.alpha * Math.PI / 180.0D;
                double sinA = Math.Sin(alphaR);
                double cosA = Math.Cos(alphaR);

                double initXX = ((vectShapesP[i].XX.X - Xmin) * scalaX) + Xoff;
                double initYY = ((vectShapesP[i].XX.Y - Ymin) * scalaY) + Yoff;
                double XX = initXX * cosA - initYY * sinA;
                double YY = initXX * sinA + initYY * cosA;

                Pnt.X = (int)XX;
                Pnt.Y = (int)YY;

                //               Pnt.X = (int)((vectShapesP[i].XX.X - Xmin) * scalaX) + Xoff;
                //             Pnt.Y = (int)((vectShapesP[i].XX.Y - Ymin) * scalaY) + Yoff;

                ctrl.Location = Pnt;
                ctrl.Size = sz;
                //       ctrl.testo = stx;
                DragDropHandler.RegisterControl(ctrl, false, false);
                //         DragDrop1.Form1.m_IsDragging = true;
                ctx = ctrl;
                //             MessageBox.Show(stx + "   " + pt.X.ToString() + "  " + pt.Y.ToString());

            }
            else MessageBox.Show("I'm sorry");
        }

        private void selezionePoint1(Point Hit)
        {

            i_selected = -1;
            HitM = Hit;
            SelezPoint.iniz = 1;
            SelezPoint dlg = new SelezPoint();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                panel1.Invalidate();
            }
        }

        private void selezioneArea(PointF Hit)
        {

            int isel = 0;

            int isel_max = 200;
            ipt = new int[isel_max];
            int i = 0;
            for (i = 0; i < nShapesA; ++i)
            {
                int pntSimb = vectShapesA[i].pntSimb;
                if (vectA[pntSimb].visib == 0) continue;
                if (Hit.X < vectShapesA[i].PointA.X) continue;
                if (Hit.X > vectShapesA[i].PointB.X) continue;
                if (Hit.Y < vectShapesA[i].PointA.Y) continue;
                if (Hit.Y > vectShapesA[i].PointB.Y) continue;
                vectShapesA[i].selected = 1;

                ipt[isel] = i;
                ++isel;
                if (isel == isel_max)
                {
                    MessageBox.Show("superato limite vettore ipt :" + isel_max.ToString());
                    break;
                }
            }

            idiff = (int)(18.0F / scalaX);

            int X1 = (int)(Hit.X - idiff);
            int Y1 = (int)(Hit.Y - idiff);
            int X2 = (int)(Hit.X + idiff);
            int Y2 = (int)(Hit.Y + idiff);

            int nsel = 0;

            for (i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                int nVertices = vectShapesA[j].nVertices;
                int ipnt = vectShapesA[j].pntPA;
                int pntSimb = vectShapesA[j].pntSimb;

                if (vectA[pntSimb].selected == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int visib = vectA[pntSimb].visib;
                if (visib == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int k = 0;
                for (k = 0; k < nVertices; ++k)
                {
                    int i1 = 0;
                    if (PA[ipnt].X > X1 && (PA[ipnt].X < X2)) ++i1;
                    if (PA[ipnt].Y > Y1 && (PA[ipnt].Y < Y2)) ++i1;
                    if (i1 == 2) break;
                    ++ipnt;

                }
                if (k < nVertices) ++nsel;
                else ipt[i] = -j;


            }

            //        MessageBox.Show(isel.ToString() + "  " + nsel.ToString());
            if (nsel == 0)
            {
                MessageBox.Show("non trovata nessuna area che comprenda il punto selezionato");
                panel1.Invalidate();
                return;
            }

            dc0.RotateTransform(alpha);

            int iX1 = (int)((X1 - Xmin) * scalaX) + Xoff;
            int iY1 = (int)((Y2 - Ymin) * scalaY) + Yoff;
            //          MessageBox.Show(X1.ToString() + "  " + Y1.ToString() + "  " + Y2.ToString());
            Rectangle pntR = new Rectangle(iX1, iY1, 2 * idiff, 2 * idiff);
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawRectangle(iPen, pntR);

            for (i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawArea(j, 0);

            }
            dc0.ResetTransform();
            //          MessageBox.Show("nsel ;  " + nsel.ToString());
            if (nsel > 1)
            {
                isel_selected = isel;
                idiff = (int)(10.0F / scalaX);
                return;
            }

            if (nsel == 1)
            {

                string caption = "selezione area";
                var result = MessageBox.Show("è l'area cercata?", caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    int jj = 0;
                    for (i = 0; i < isel; ++i)
                    {
                        jj = ipt[i];
                        if (jj < 0) continue;
                        break;
                    }
                    selected_area = jj;
                    //                   AreaSelect_flag = 1;
                    Select_flag = (int)flagSelez.area;

                    panel1.Invalidate();
                    isel_selected = 0;
                }
                else
                {
                    panel1.Invalidate();
                }

            }
        }

        private void selezioneArea1(Point Hit)
        {
            int isel = isel_selected;

            dc0.RotateTransform(alpha);

            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawArea(j, 1);
            }
            dc0.ResetTransform();

            idiff -= (int)(3.0F / scalaX);

            int X1 = Hit.X - idiff;
            int Y1 = Hit.Y - idiff;
            int X2 = Hit.X + idiff;
            int Y2 = Hit.Y + idiff;

            int nsel = 0;
            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                int nVertices = vectShapesA[j].nVertices;
                int ipnt = vectShapesA[j].pntPA;
                int pntSimb = vectShapesA[j].pntSimb;

                if (vectA[pntSimb].selected == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int visib = vectA[pntSimb].visib;
                if (visib == 0)
                {
                    ipt[i] = -j;
                    continue;
                }
                int k = 0;
                for (k = 0; k < nVertices; ++k)
                {
                    int i1 = 0;
                    if (PA[ipnt].X > X1 && (PA[ipnt].X < X2)) ++i1;
                    if (PA[ipnt].Y > Y1 && (PA[ipnt].Y < Y2)) ++i1;
                    if (i1 == 2) break;
                    ++ipnt;

                }
                if (k < nVertices) ++nsel;
                else ipt[i] = -j;


            }

            //        MessageBox.Show(isel.ToString() + "  " + nsel.ToString());
            if (nsel == 0)
            {
                MessageBox.Show("non trovata nessuna linea che comprenda il punto selezionato - 2");
                isel_selected = 0;
                tipo_selezione = 0;
                panel1.Invalidate();
                return;
            }

            dc0.RotateTransform(alpha);

            Point Pt = new Point();
            Pt.X = (int)((X1 - Xmin) * scalaX) + Xoff;
            Pt.Y = (int)((Y2 - Ymin) * scalaY) + Yoff;
            Size sz = new Size(2 * idiff, 2 * idiff);
            Rectangle pntR = new Rectangle(Pt, sz);
            Pen iPen = new Pen(Color.Red, 1);
            dc0.DrawRectangle(iPen, pntR);

            for (int i = 0; i < isel; ++i)
            {
                int j = ipt[i];
                if (j < 0) continue;
                drawArea(j, 0);

            }
            dc0.ResetTransform();


            if (nsel == 1)
            {

                string caption = "selezione linea";
                var result = MessageBox.Show("è la linea cercata?", caption,
                              MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    int jj = 0;
                    for (int i = 0; i < isel; ++i)
                    {
                        jj = ipt[i];
                        if (jj > 0) break;
                    }
                    selected_area = jj;
                    Select_flag = (int)flagSelez.area;
                    panel1.Invalidate();
                    isel_selected = 0;
                }

            }

            if (nsel > 1)
            {
                isel_selected = nsel;

                lpt = new string[nsel];
                int j = 0;
                for (int i = 0; i < isel; ++i)
                {
                    int jj = ipt[i];
                    if (jj < 0) continue;
                    lpt[j] = jj.ToString();
                    ++j;
                }


                InfoDialog10 dlg = new InfoDialog10();
                DialogResult iret = dlg.ShowDialog();
                if (iret == DialogResult.OK)
                {

                    selected_area = int.Parse(lpt[selElem]);
                    Select_flag = (int)flagSelez.area;
                    panel1.Invalidate();
                    isel_selected = 0;
                    tipo_selezione = 0;

                }
                else
                {
                    isel_selected = 0;
                    tipo_selezione = 0;
                    Select_flag = 0;
                    panel1.Invalidate();
                }
            }
        }

        private void drawArea(int ipt, int opt)
        {
            char[] delimiterChars0 = { ',', '/' };
            string[] words;


            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int nVertices = vectShapesA[ipt].nVertices;
            int ipnt = vectShapesA[ipt].pntPA;

            int nParts = vectShapesA[ipt].nParts;
            int[] ipart = new int[nParts + 1];
            if (nParts == 1)
            {
                ipart[0] = 0;
                ipart[1] = nVertices;
            }
            else
            {
                string sp = vectShapesA[ipt].Parts_string;
                words = sp.Split(delimiterChars0);
                for (int j = 0; j < nParts; ++j) ipart[j] = int.Parse(words[j]);
                ipart[nParts] = nVertices;
            }
            int pntSimb = vectShapesA[ipt].pntSimb;

            if (vectA[pntSimb].selected == 0) goto L40;
            int visib = vectA[pntSimb].visib;
            if (visib == 0) goto L40;

            //             Color icolor = vectA[pntSimb].icolor;

            Color icolor = new Color();
            if (opt == 1) icolor = vectA[pntSimb].icolor;
            else icolor = Color.Red;

            int type = vectA[pntSimb].codice_bordo;
            string ss = vectA[pntSimb].bordo;

            string s1;
            int ipos;
            float[] dashValues = { 0, 0 };
            //              float dist_linee = 0;
            //              float offset = 0;
            float width = 0F;
            switch (type)
            {
                case 1:
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    width = float.Parse(s1);
                    break;
                case 5:
                    ipos = ss.IndexOf('/');
                    s1 = ss.Substring(0, ipos);
                    words = s1.Split(delimiterChars0);
                    width = float.Parse(words[2], nfi);
                    dashValues[0] = float.Parse(words[0], nfi);
                    dashValues[1] = float.Parse(words[1], nfi);
                    break;
                default:
                    width = 0;
                    break;
            }

            if (width == 0F) goto L40;

            if (icolor == Color.Red) width += 1;
            Pen iPen = new Pen(icolor, width);
            if (dashValues[0] > 0F) iPen.DashPattern = dashValues;


            Point[] polygon = new Point[1];

            for (int k = 0; k < nParts; ++k)
            {
                ipnt = vectShapesA[ipt].pntPA + ipart[k];
                int iVertices = ipart[k + 1] - ipart[k];
                polygon = new Point[iVertices];
                for (int j = 0; j < iVertices; ++j)
                {
                    polygon[j].X = (int)((PA[ipnt].X - Xmin) * scalaX) + Xoff;
                    polygon[j].Y = (int)((PA[ipnt].Y - Ymin) * scalaY) + Yoff;
                    Rectangle pntR = new Rectangle(polygon[j].X, polygon[j].Y, 4, 4);
                    dc0.DrawEllipse(iPen, pntR);
                    if (j == 0)
                    {
                        Rectangle pntR1 = new Rectangle(polygon[j].X, polygon[j].Y, 8, 8);
                        dc0.DrawEllipse(iPen, pntR1);
                    }
                    ++ipnt;
                }
                dc0.DrawCurve(iPen, polygon);
            }
        L40: ;
        }
   
        private int suddivisione_linea()
        {

            int last_linea = nShapes - 1;
            if (last_linea + splitNodo > max_nShapes)
            {
                MessageBox.Show("eccessivo numero di linee: ");
                return (-1);
            }

            int nVertices = vectShapes[last_linea].nVertices;
            int lastPnt = vectShapes[last_linea].pntpL + nVertices;
            int lastPnt1 = lastPnt;

            nVertices = vectShapes[selected_linea].nVertices;
            int new_points = lastPnt + nVertices;
            if (new_points > max_nPoints)
            {
                int maxP = max_nPoints - lastPnt;
                int newP = vectShapes[selected_linea].nVertices + vectShapes[isel_selected].nVertices;
                MessageBox.Show("eccessivo numero di nuovi punti  " + newP.ToString() + " / " + maxP.ToString());
                return(-1);
            }

            int lPnt = vectShapes[selected_linea].pntpL;
            for (int i = 0; i < nVertices; ++i)
            {
                PL[lastPnt] = PL[lPnt];
                ++lPnt;
                ++lastPnt;
            }

            if (splitNodo > 1 && split_points[0] > split_points[1])
            {
                int[] save = new int[10];
                                                  
                int lpt1 = splitNodo - 1;
                for (int i = 0; i < splitNodo; ++i)
                {
                    save[i] = split_points[lpt1];
                    --lpt1;
                }

                for (int i = 0; i < splitNodo; ++i)
                {
                    split_points[i] = save[i];
                }
            }

            int numID = vectShapes[nShapes-1].numID;
            ++numID;

            int ipt = nShapes;
            vectShapes[ipt].numID = numID;
            vectShapes[ipt].nVertices = split_points[0] + 1;
            vectShapes[ipt].pntSimb = vectShapes[selected_linea].pntSimb;
            vectShapes[ipt].pntpL = lastPnt1;
            vectShapes[ipt].modifica = 3;
            vectShapes[ipt].edizione = "3";
            vectShapes[ipt].tipo = 0;
            vectShapesPointAB(ipt);
            vectShapesQuota(ipt);
            vectShapesLunghezza(ipt);
            ++ipt;
            ++numID;

            vectShapes[ipt].numID = numID;
            if (splitNodo == 1) vectShapes[ipt].nVertices = vectShapes[selected_linea].nVertices - split_points[0];
            else vectShapes[ipt].nVertices = split_points[1] - split_points[0] + 1;
            vectShapes[ipt].pntpL = lastPnt1 + split_points[0];
            vectShapes[ipt].pntSimb = vectShapes[selected_linea].pntSimb;

            vectShapes[ipt].modifica = 3;
            vectShapes[ipt].edizione = "3";
            vectShapes[ipt].tipo = 0;
  
            vectShapesPointAB(ipt);
            vectShapesQuota(ipt);
            vectShapesLunghezza(ipt);
            ++ipt;
            ++numID;

            if (splitNodo > 1)
            {
                int splitNodo1 = splitNodo - 1;
                for (int i = 1; i < splitNodo; ++i)
                {
                    vectShapes[ipt].numID = numID;
                    vectShapes[ipt].pntpL = lastPnt1 + split_points[i];
                    vectShapes[ipt].pntSimb = vectShapes[selected_linea].pntSimb;
                    if (i < splitNodo1) vectShapes[ipt].nVertices = split_points[i] - split_points[i - 1] + 1;
                    else vectShapes[ipt].nVertices = vectShapes[selected_linea].nVertices - split_points[i];

                    vectShapes[ipt].modifica = 3;
                    vectShapes[ipt].edizione = "3";
                    vectShapes[ipt].tipo = 0;


                    vectShapesPointAB(ipt);
                    vectShapesQuota(ipt);
                    vectShapesLunghezza(ipt);
                    ++ipt;
                    ++numID;
                }
            }
            Save.modificaLinea = 1;
            int splitNodo2 = splitNodo + 1;
            nShapes += splitNodo2;
            string ss = "sentiero " + selected_linea.ToString() + " suddiviso in " + splitNodo2.ToString() + 
                " nuovi sentieri, si vuole cancellare il sentiero " + selected_linea.ToString()+ " ?";

            string caption = "suddivisione sentiero";
            var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if(vectShapes[selected_linea].modifica == 3) vectShapes[selected_linea].modifica = 5;
                else
                {
                    vectShapes[selected_linea].modifica = 2;
                }
                
            }
     
            return (0);
        }

        public static void vectShapesPointAB(int ipt)
        {

            int nVertices = ViewQ.vectShapes[ipt].nVertices;

            int pntl = ViewQ.vectShapes[ipt].pntpL;
            int Xmin = ViewQ.PL[pntl].X;
            int Xmax = ViewQ.PL[pntl].X;
            int Ymin = ViewQ.PL[pntl].Y;
            int Ymax = ViewQ.PL[pntl].Y;
            for (int j = 1; j < nVertices; ++j)
            {
                if (Xmin > ViewQ.PL[pntl].X) Xmin = ViewQ.PL[pntl].X;
                if (Xmax < ViewQ.PL[pntl].X) Xmax = ViewQ.PL[pntl].X;
                if (Ymin > ViewQ.PL[pntl].Y) Ymin = ViewQ.PL[pntl].Y;
                if (Ymax < ViewQ.PL[pntl].Y) Ymax = ViewQ.PL[pntl].Y;
                ++pntl;
            }
            ViewQ.vectShapes[ipt].PointA.X = Xmin;
            ViewQ.vectShapes[ipt].PointA.Y = Ymin;
            ViewQ.vectShapes[ipt].PointB.X = Xmax;
            ViewQ.vectShapes[ipt].PointB.Y = Ymax;

        }

        public static void unione_linee()
        {

            int last_linea = nShapes - 1;
            int nVertices = vectShapes[last_linea].nVertices;
            int lastPnt = vectShapes[last_linea].pntpL + nVertices;
  
            int inum = ElencoSentieri.inum;
            nVertices = 0;
            
            for(int i=0;i<inum;++i)
            {
                int ipt = ElencoSentieri.numSelez[i];
                nVertices += vectShapes[ipt].nVertices;
            }
            int new_points = lastPnt + nVertices;
            if(new_points > max_nPoints )
            { 
                int maxP = max_nPoints - lastPnt;
                int newP = vectShapes[selected_linea].nVertices + vectShapes[isel_selected].nVertices;
                MessageBox.Show("eccessivo numero di nuovi punti  " + newP.ToString() +" / " + maxP.ToString());
                return ;
            }

            int nVertTotal = nVertices;

             for (int i = 0; i < inum - 1; ++i)
             {

                 int ipt = ElencoSentieri.numSelez[i];
                 int ipt1 = ElencoSentieri.numSelez[i + 1];
                 int pntl1 = vectShapes[ipt].pntpL;
                 int pntl2 = vectShapes[ipt1].pntpL;
                 double distX = PL[pntl2].X - PL[pntl1].X;
                 double distY = PL[pntl2].Y - PL[pntl1].Y;
                 double dist1_1 = Math.Sqrt(distX * distX + distY * distY);

                 nVertices = vectShapes[ipt1].nVertices;
                 pntl2 = pntl2 + nVertices - 1;
                 distX = PL[pntl2].X - PL[pntl1].X;
                 distY = PL[pntl2].Y - PL[pntl1].Y;
                 double dist1_2 = Math.Sqrt(distX * distX + distY * distY);

                 nVertices = vectShapes[ipt].nVertices;
                 pntl1 = pntl1 + nVertices - 1;
                 pntl2 = vectShapes[ipt1].pntpL;
                 distX = PL[pntl2].X - PL[pntl1].X;
                 distY = PL[pntl2].Y - PL[pntl1].Y;
                 double dist2_1 = Math.Sqrt(distX * distX + distY * distY);

                 nVertices = vectShapes[ipt1].nVertices;
                 pntl2 = pntl2 + nVertices - 1;
                 distX = PL[pntl2].X - PL[pntl1].X;
                 distY = PL[pntl2].Y - PL[pntl1].Y;
                 double dist2_2 = Math.Sqrt(distX * distX + distY * distY);

//
//                    i             i+1
//              1           2  1           2        option = 1
//              -------------  ------------
//              1           2  2           1        option = 2
//              -------------  ------------
//              2           1  1           2        option = 3
//              -------------  ------------
//              2           1  2           1        option = 4
//              -------------  ------------

                 int option = 0;
                 if (dist2_1 < dist1_1 && dist2_1 < dist2_2) option = 1;
                 if (dist2_1 < dist1_1 && dist2_1 > dist2_2) option = 2;
                 if (dist2_1 > dist1_1 && dist2_1 < dist2_2) option = 3;
                 if (dist2_1 > dist1_1 && dist2_1 > dist2_2) option = 4;

                 if(i == 0 )
                 {
                     int lpt = ElencoSentieri.numSelez[i];
                     nVertices = vectShapes[lpt].nVertices;
                     if (option == 1 || option == 2)
                     {
                         int lPnt = vectShapes[lpt].pntpL;
                         for (int j = 0; j < nVertices; ++j)
                         {
                             PL[lastPnt] = PL[lPnt];
                             ++lastPnt;
                             ++lPnt;
                         }
                     }
                    if(option == 3 || option == 4)
                    {
                        int lPnt = vectShapes[lpt].pntpL + nVertices - 1;
                        for(int j = 0;j<nVertices;++j)
                        {
                            PL[lastPnt] = PL[lPnt];
                            ++lastPnt;
                            --lPnt;
                        }
                    }
                 }

                 if(option == 1 || option == 2)
                 {
                     int lpt = ElencoSentieri.numSelez[i+1];
                     nVertices = vectShapes[lpt].nVertices;
                     int lPnt = vectShapes[lpt].pntpL;
                     for (int j = 0; j < nVertices; ++j)
                     {
                         PL[lastPnt] = PL[lPnt];
                         ++lastPnt;
                         ++lPnt;
                     }
                 }

                 if (option == 3 || option == 4)
                 {
                     int lpt = ElencoSentieri.numSelez[i+1];
                     nVertices = vectShapes[lpt].nVertices;
                     int lPnt = vectShapes[lpt].pntpL + nVertices - 1;
                     for (int j = 0; j < nVertices; ++j)
                     {
                         PL[lastPnt] = PL[lPnt];
                         ++lastPnt;
                         --lPnt;
                     }
                 }
             }

             int mpt = nShapes;
            nVertices = vectShapes[mpt -1].nVertices;

            vectShapes[mpt].pntpL = vectShapes[mpt - 1].pntpL + nVertices;
            vectShapes[mpt].nVertices = nVertTotal;
            vectShapes[mpt].modifica = 3;
            vectShapesPointAB(mpt);
            vectShapesQuota(mpt);
            vectShapesLunghezza(mpt);
            Save.modificaLinea = 1;

            int ipt2 = ElencoSentieri.numSelez[0];
            vectShapes[mpt].pntSimb = vectShapes[ipt2].pntSimb;
            vectShapes[mpt].edizione ="3";
            int numID = vectShapes[mpt - 1].numID;
            ++numID;
            vectShapes[mpt].numID = numID;
            nShapes += 1;

            string ss = string.Empty;
            for (int i = 0; i < inum; ++i)
            {
                int ipt = ElencoSentieri.numSelez[i];
                ss += vectShapes[ipt].numID.ToString() ;
                if (i < inum - 1) ss += " + ";
            }

            ss = "creato il nuovo sentiero " + numID.ToString() + " = " + ss;

            ss += ", si vuole cancellare i sentieri originari ?";

            string caption = "unione sentieri";
            var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                for (int i = 0; i < inum; ++i)
                {
                    int ipt = ElencoSentieri.numSelez[i];
                    vectShapes[ipt].modifica = 2;
                }
            }

        }
        
        public static void vectShapesQuota(int ipt)
        {

            vectShapes[ipt].quota = "0";

        }

        public static void vectShapesLunghezza(int ipt)
        {
            int nVertices = ViewQ.vectShapes[ipt].nVertices;
            double dlen = 0;
            int pntl = ViewQ.vectShapes[ipt].pntpL;
            int pntl1 = pntl + 1;
            for (int j = 1; j < nVertices; ++j)
            {
                double dx = ViewQ.PL[pntl1].X - ViewQ.PL[pntl].X;
                double dy = ViewQ.PL[pntl1].Y - ViewQ.PL[pntl].Y;
                double dd = Math.Sqrt(dx * dx + dy * dy);
                dlen += dd;
                ++pntl;
                ++pntl1;
            }
            vectShapes[ipt].lunghezza = dlen.ToString("F");

        }

        private void riordino_nodi()
        {
            if (splitNodo == 1) return;
            if (splitNodo == 2)
            {
                int first = split_points[0];
                if (split_points[1] > first) return;
                split_points[0] = split_points[1];
                split_points[1] = first;
            }
        }


        public static void esportazione_txt()
        {
            string fileNameX = string.Concat(FormIniz.cartellaGPS, FormIniz.nameElement);
            fileNameX += "_" + selected_linea.ToString() + ".txt";
            SaveFileDialog textDialog = new SaveFileDialog();
            textDialog.Filter = "Text Files | *.txt";
            textDialog.DefaultExt = "txt";
            textDialog.FileName = fileNameX;  // ViewQ.selectedElement;
            textDialog.InitialDirectory = FormIniz.cartellaGPS;
            if (DialogResult.OK == textDialog.ShowDialog())
            {
                StreamWriter sw1 = new StreamWriter(fileNameX);
                int nVertices = vectShapes[selected_linea].nVertices;
                string s1 = FormIniz.nameElement;
                s1 += " " + nVertices.ToString();
                sw1.WriteLine(s1);
                int ipnt = vectShapes[selected_linea].pntpL;
                for (int i = 0; i < nVertices;++i)
                {
                    s1 = PL[ipnt].X.ToString() + " " + PL[ipnt].Y.ToString();
                    sw1.WriteLine(s1);
                    ++ipnt;
                }
                
                sw1.Close();
                
            }
  
        }

        static public void importa_linee()
        {
            OpenFileDialog textDialog = new OpenFileDialog();
            textDialog.Filter = "Text Files | *.txt";
            textDialog.DefaultExt = "txt";
            //          textDialog.FileName = FormIniz.cartellaGPS;
            textDialog.InitialDirectory = FormIniz.cartellaGPS;
            if (DialogResult.OK == textDialog.ShowDialog())
            {
                string fileNameX = textDialog.FileName;

                StreamReader sf = File.OpenText(fileNameX);
                string s1 = sf.ReadLine();
                string[] words;

                char[] delimiterChars = { ' ' };

                words = s1.Split(delimiterChars);

                string elem = words[0];
                int iVertices = int.Parse(words[1]);

                int last_linea = nShapes - 1;
                int nVertices = vectShapes[last_linea].nVertices;
                int lastPnt = vectShapes[last_linea].pntpL + nVertices;

                int new_points = lastPnt + iVertices;
                if (new_points > max_nPoints)
                {
                    MessageBox.Show("eccessivo numero di nuovi punti  " + new_points.ToString() + " / " + max_nPoints.ToString());
                    return;
                }


                int ipnt = lastPnt;
                for (int i = 0; i < iVertices; ++i)
                {
                    s1 = PL[ipnt].X.ToString() + " " + PL[ipnt].Y.ToString();
                    s1 = sf.ReadLine();
                    words = s1.Split(delimiterChars);
                    PL[ipnt].X = int.Parse(words[0]);
                    PL[ipnt].Y = int.Parse(words[1]);
                    ++ipnt;
                }

                sf.Close();

                int mpt = nShapes;

                vectShapes[mpt].pntpL = vectShapes[mpt - 1].pntpL + nVertices;
                vectShapes[mpt].nVertices = iVertices;
                vectShapes[mpt].modifica = 3;
                vectShapesPointAB(mpt);
                vectShapesQuota(mpt);
                vectShapesLunghezza(mpt);
                Save.modificaLinea = 1;

                int pntSimb = -1;
                string stcl = "5L000SE";
                for (int i = 0; i < ViewQ.nSimb; ++i)
                {
                    if (ViewQ.vectS[i].codice.Contains(stcl))
                    {
                        pntSimb = i;
                        break;
                    }
                }
                if (pntSimb == -1)
                {
                    string ss2 = string.Empty;

                    using (SQLiteConnection conn = SetConnection1())
                    {
                        conn.Open();
                        using (SQLiteCommand cmd = new SQLiteCommand(conn))
                        {
                            cmd.CommandText = "select * from linea where codice_stclasse = '" + stcl + "'";
                            SQLiteDataReader reader1 = cmd.ExecuteReader();
                            if (reader1.HasRows)
                            {
                                while (reader1.Read())
                                {
                                    pntSimb = ViewQ.nSimb;
                                    ViewQ.vectS[pntSimb].codice = reader1.GetString(0).ToString();
                                    ss2 = reader1.GetString(1).ToString();
                                    ViewQ.vectS[pntSimb].denom = reader1.GetString(2).ToString();
                                    string ss1 = reader1.GetString(3).ToString();
                                    ViewQ.vectS[pntSimb].penType = int.Parse(ss1);
                                    ViewQ.vectS[pntSimb].tratto = reader1.GetString(4).ToString();
      //                            string[] words;
                                    char[] delimiterChars0 = { ' ', ',' };
                                    ss1 = reader1.GetString(5).ToString();

                                    words = ss1.Split(delimiterChars0);
                                    byte b1 = byte.Parse(words[0]);
                                    byte b2 = byte.Parse(words[1]);
                                    byte b3 = byte.Parse(words[2]);
                                    byte b4 = byte.Parse(words[3]);
                                    ViewQ.vectS[pntSimb].icolor = Color.FromArgb(b1, b2, b3, b4);

                                    ViewQ.vectS[pntSimb].visib = 1;
                                    ViewQ.vectS[pntSimb].selected = 1;

                                    ViewQ.nSimb += 1;
                                }
                            }
                            else
                            {
                                MessageBox.Show("non si trovano le proprietà per la linea " + stcl);
                                return;

                            }
                        }

                        conn.Close();
                    }
                }

                vectShapes[mpt].pntSimb = pntSimb;

                vectShapes[mpt].edizione = "3";
                int numID = vectShapes[mpt - 1].numID;
                ++numID;
                vectShapes[mpt].numID = numID;
                nShapes += 1;
            }

        }



    }
}
